Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
passport-custom
Advanced tools
The passport-custom npm package allows you to create custom authentication strategies for Passport.js. This is useful when you need to implement authentication mechanisms that are not covered by the existing Passport.js strategies.
Custom Authentication Strategy
This feature allows you to define a custom authentication strategy. In this example, the strategy checks for a user in the database and verifies the password.
const passport = require('passport');
const CustomStrategy = require('passport-custom').Strategy;
passport.use('custom', new CustomStrategy(
function(req, done) {
User.findOne({ username: req.body.username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(req.body.password)) { return done(null, false); }
return done(null, user);
});
}
));
Integration with Express
This feature demonstrates how to integrate the custom authentication strategy with an Express application. The '/login' route uses the custom strategy for authentication.
const express = require('express');
const passport = require('passport');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(passport.initialize());
app.post('/login',
passport.authenticate('custom', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
The passport-local package is a Passport.js strategy for authenticating with a username and password. It is similar to passport-custom in that it allows for custom authentication logic, but it is specifically designed for local authentication using a username and password.
The passport-oauth2 package is a Passport.js strategy for authenticating with OAuth 2.0. It is similar to passport-custom in that it allows for custom authentication logic, but it is specifically designed for OAuth 2.0 authentication.
The passport-http package is a Passport.js strategy for HTTP Basic and Digest authentication. It is similar to passport-custom in that it allows for custom authentication logic, but it is specifically designed for HTTP Basic and Digest authentication.
Passport strategy for authenticating with custom logic.
This module lets you authenticate using custom logic in your Node.js applications. It is based on passport-local module by Jared Hanson. By plugging into Passport, custom authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
$ npm install passport-custom
The custom authentication strategy authenticates users by custom logic of your choosing.
The strategy requires a verify
callback, which is where the custom logic goes and calls
done
providing a user. Note that, req is always passed as the first parameter to the
verify
callback.
Here is the pseudo code:
import passportCustom from 'passport-custom';
const CustomStrategy = passportCustom.Strategy;
passport.use('strategy-name', new CustomStrategy(
function(req, callback) {
// Do your custom user finding logic here, or set to false based on req object
callback(null, user);
}
));
And a basic example:
passport.use(new CustomStrategy(
function(req, done) {
User.findOne({
username: req.body.username
}, function (err, user) {
done(err, user);
});
}
));
Use passport.authenticate()
, specifying the 'custom'
strategy (or whatever you named the strategy upon registration), to
authenticate requests.
For example, as route middleware in an Express application:
app.post('/login',
passport.authenticate('custom', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);
$ npm install
$ npm test
Copyright (c) 2014-2015 Mike Bell
FAQs
Custom authentication strategy for Passport.
The npm package passport-custom receives a total of 227,148 weekly downloads. As such, passport-custom popularity was classified as popular.
We found that passport-custom demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.